package com.lzx.applib.utils;

import android.content.Context;

public class DpSpDip2Px {

	static boolean init = false;

	static float scale;
	static float fontScale;
	static int screenPxWidth;
	static float screenDpWidth;
	static float screenDpHigh;
	
	private DpSpDip2Px() {}
	
	public static void init(Context context) {
		scale = context.getResources().getDisplayMetrics().density;
		fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		screenPxWidth = context.getResources().getDisplayMetrics().widthPixels;
		screenDpHigh = context.getResources().getDisplayMetrics().heightPixels;

		init = true;
	}

	public static DpSpDip2Px instance = new DpSpDip2Px();
	public static DpSpDip2Px getInstance() {
		if (!init) {
//			throw new IllegalStateException("DpSpDip2Px should be inited before.");
		}
		return instance;
	}

	public int getScreenDpWidth() {
		return px2dip(screenPxWidth);
	}

	public int dip2px(float dpValue) {
		return (int) (dpValue * scale + 0.5f);
	}

	public int px2dip(float pxValue) {
		return (int) (pxValue / scale + 0.5f);
	}

	public int px2sp(float pxValue) {
		return (int) (pxValue / fontScale + 0.5f);
	}

	public int sp2px(float spValue) {
		return (int) (spValue * fontScale + 0.5f);
	}

	public int sp2dp(float spValue) {
		float pxValue = spValue * fontScale + 0.5f;
		return (int) (pxValue / scale + 0.5f);
	}

}
